package com.aios.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.aios.dao.SwitchMapper;
import com.aios.dao.SwitchPanelMapper;
import com.aios.dao.SystemUserHasSwitchPanelMapper;
import com.aios.entity.Switch;
import com.aios.entity.SwitchPanel;
import com.aios.entity.SystemUserHasSwitchPanel;
import com.aios.service.ExistValidationEnum;
import com.aios.service.SwitchPanelService;
import com.aios.util.GuidGenerator;

@Service("switchPanelService")
public class SwitchPanelServiceImpl extends AbstractService implements
		SwitchPanelService {

	@Autowired private SwitchPanelMapper switchPanelMapper;

	@Autowired private SwitchMapper switchMapper;
	
	@Autowired private SystemUserHasSwitchPanelMapper systemUserHasSwitchPanelMapper;

	@Override public List<SwitchPanel> findAll() {
		return null;
	}

	@Override
	public List<SwitchPanel> findAllNotDisabled() {
		return null;
	}

	@Override
	public List<SwitchPanel> findAllDisabled() {
		return null;
	}

	@Override
	public SwitchPanel findById(String id) {
		return switchPanelMapper.findById(id);
	}

	@Override
	public int save(SwitchPanel entity) {
		return 0;
	}

	@Override
	public int update(SwitchPanel entity) {
		return switchPanelMapper.updateByPrimaryKeySelective(entity);
	}

	@Override
	public int delete(SwitchPanel id) {
		return 0;
	}

	@Override
	public int delete(String id) {
		return 0;
	}

	@Override
	public int save(SwitchPanel entity, String userId) {
		int returnVal = 0;
		saveAddSettings(entity, userId);
		entity.setId(GuidGenerator.switchPanelKey());
		returnVal = switchPanelMapper.insert(entity);
		return returnVal;
	}

	@Override
	public int update(SwitchPanel entity, String userId) {
		return 0;
	}

	@Override
	public List<SwitchPanel> findControllerByUserId(String id) {
		return null;
	}

	@Override
	public ExistValidationEnum nameExistValidation(String name) {
		return null;
	}

	@Override
	public List<SwitchPanel> findByUserId(String userId) {
		return switchPanelMapper.findByUserId(userId);
	}

	@Override
	public int savePanelWithSwitches(SwitchPanel entity, String userId, String systemUserId) {
		
		int returnVal = 0;

		entity.setName("SwitchPanel");
		
		entity.setUserName(SwitchPanel.DEFAULT_USER_NAME);
		entity.setUserPassword(SwitchPanel.DEFAULT_PASSWORD);
		entity.setProtocol(SwitchPanel.DEFAULT_PROTOCOL);
		
		returnVal = this.save(entity, systemUserId);
		
		SystemUserHasSwitchPanel record = new SystemUserHasSwitchPanel();
		record.setSystemUserId(userId);
		record.setSwitchPanelId(entity.getId());
		saveAddSettings(record, systemUserId);
		returnVal += systemUserHasSwitchPanelMapper.insert(record);

		for (int i = 1; i <= Switch.NUMBER_OF_SWITCHES; i++) {
			
			Switch sw = new Switch();
			
			sw.setId(GuidGenerator.switchKey());
			sw.setName(Switch.DEFAULT_LABEL_PREFIX + i);
			sw.setControllerParameter(String.valueOf(i));
			sw.setControllerReceiver(Switch.PREFIX_LABEL + String.format("%02d", i));
			sw.setSwitchPanelId(entity.getId());
			saveAddSettings(sw, userId);
			returnVal += switchMapper.insert(sw);
		}
		
		return returnVal;
	}
}
